//
//  YCTabBarViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/2/20.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCTabBarViewController.h"

#define SCREEN_SIZE [UIScreen mainScreen].bounds.size

@interface YCTabBarViewController ()
{
    NSMutableArray *_tabButtons;
}
@end

@implementation YCTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabBar setBackgroundImage:[UIImage new]];
//    [self.tabBar setShadowImage:[UIImage new]];
    
    _tabButtons=[NSMutableArray array];
    [self setTabbar];
}

-(void)setTabbar
{
    YCOrderDealViewController   *dealVC   = [[YCOrderDealViewController alloc]init];
    YCOrderQueryViewController    *queryVC    = [[YCOrderQueryViewController alloc]init];
    YCManageViewController *manageVC = [[YCManageViewController alloc]init];
    YCSetViewController      *setVC      = [[YCSetViewController alloc]init];
    
    YCNavigationViewController *navi1 = [[YCNavigationViewController alloc]initWithRootViewController:dealVC];
    [navi1.alphaView addShadow];
    navi1.navigationItem.title = @"订单处理";
    YCNavigationViewController *navi2 = [[YCNavigationViewController alloc]initWithRootViewController:queryVC];
    [navi2.alphaView addShadow];
    navi2.navigationItem.title = @"订单查询";
    
    YCNavigationViewController *navi3 = [[YCNavigationViewController alloc]initWithRootViewController:manageVC];
    
    YCNavigationViewController *navi4 = [[YCNavigationViewController alloc]initWithRootViewController:setVC];
    navi4.alphaView.backgroundColor = MAIN_COLOR;
    navi4.navigationItem.title = @"设置";
    self.viewControllers = @[navi1,navi2,navi3,navi4];
   
    [self createTabbarButton];
    self.selectedViewController = navi1;
}
- (void)createTabbarButton {
    //在系统的Tabbar上添加UIImageView控件
    UIImageView *barImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 49)];
    barImageView.backgroundColor = [UIColor whiteColor];
    barImageView.layer.shadowOpacity = 0.2;
    barImageView.layer.shadowColor = LIGHT_COLOR.CGColor;
    barImageView.layer.shadowRadius = 2;
    barImageView.layer.shadowOffset = CGSizeMake(0,0);
    
    float wide = barImageView.bounds.size.width;
    float height = barImageView.bounds.size.height;
    float border=2.0f;
    
    UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(-border, -border, wide+2*border, height+2*border)];
    aPath.lineWidth = 1.0;
    aPath.lineCapStyle = kCGLineCapRound;
    aPath.lineJoinStyle = kCGLineCapRound;
    barImageView.layer.shadowPath = aPath.CGPath;
    
    barImageView.userInteractionEnabled = YES;
    [self.tabBar addSubview:barImageView];
    
    NSArray *arrayTitle=@[@"订单处理",@"订单查询",@"店铺管理",@"设置"];
    CGFloat width=SCREEN_SIZE.width / arrayTitle.count;
    
    for (int i = 0; i < self.viewControllers.count; i++) {
        YCTabBarButton *tabbarButton = [YCTabBarButton buttonWithFrame:CGRectMake(width * i, 0, width, 49) withIndex:i withTitle:arrayTitle[i]];
        tabbarButton.delegate = self;
        if (i == 0) {
            tabbarButton.choiceType = YFChoiceButtonStatusTypeSelected;
        }
        [barImageView addSubview:tabbarButton];
        [_tabButtons addObject:tabbarButton];
    }
    [self.tabBar setBackgroundImage:[self imageWithColor:[UIColor clearColor] andSize:self.tabBar.frame.size]];
    [self.tabBar setShadowImage:[self imageWithColor:[UIColor clearColor] andSize:self.tabBar.frame.size]];
}

#pragma mark - YFButtonDelegate
- (void)YFChoiceButtonSelectedIndex:(NSUInteger)index {
    self.selectedViewController = self.viewControllers[index];
}

#pragma mark - 根据颜色和大小得到一张图片
- (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}



@end








